package com.kma.summer2012.client.view.requests;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.*;
import com.kma.summer2012.client.constants.ConstantsHolder;
import com.kma.summer2012.client.styles.style2.Recources;
import com.kma.summer2012.shared.model.UIComment;
import com.kma.summer2012.shared.model.UICrumbsDateAndTime;
import com.kma.summer2012.shared.model.UICrumbsRequest;
import com.kma.summer2012.shared.model.UICrumbsWorkItem;

import java.util.ArrayList;
import java.util.Date;

public class EditRequestForManager extends Request {
    @UiTemplate("Request.ui.xml")
    interface EditRequestUiBinder extends UiBinder<HTMLPanel, Request> {
    }

    private static EditRequestUiBinder editRequestUibinder = GWT.create(EditRequestUiBinder.class);


    public EditRequestForManager() {
        super(editRequestUibinder);
        this.title.setText(constants.editRequest());
        this.id.setVisible(true);
        this.saveButton.setVisible(false);
        this.editButtonManager.setVisible(true);
        this.editButtonOperator.setVisible(false);
        this.sendForProcessingButton.setEnabled(false);

        enableField();
        this.comments.setEnabled(true);
        this.rejectButton.setVisible(true);
        this.rejectButton.setEnabled(true);
        this.assignButton.setVisible(true);
        setInnerStyle();
    }


    @Override
    public void setParametres(UICrumbsRequest request) {
        //nessessary data
        id.setText("" + request.getId());
        datelbl.setText(request.getDateOfCreation().toString());
        state.setText(request.getState());
        clAddress.setText(request.getUicrumbsClient().getAdress());
        clMail.setText(request.getUicrumbsClient().getMail());
        clName.setText(request.getUicrumbsClient().getName());
        if (request.getUicrumbsWorkItems().size() > 0) {
            for (UICrumbsWorkItem key : request.getUicrumbsWorkItems().keySet()) {
                ArrayList<String> tmp = new ArrayList<String>();
                tmp.add(key.getWorkName());
                tmp.add("" + key.getId());
                amountsOfWorkValues.put(key.getId(), (int) request.getUicrumbsWorkItems().get(key));
                Integer k = new Integer(key.getId());
                workList.put(k, key);
                TextBox txt = new TextBox();
                txt.setValue("" + request.getUicrumbsWorkItems().get(key));
                amountOfWorkInTxtBox.put(k, txt);
                this.totalSumOfWork += key.getRuntime() * request.getUicrumbsWorkItems().get(key);

            }
            addeWorkField();
        }

        if (request.getRequestInfo() != null)
            requestInfo.setText(request.getRequestInfo());
        else
            requestInfo.setText("");


        timeRequired.setText("" + totalSumOfWork);


        if (request.getUicrumbsDateAndTimes().size() > 0) {
            int index = 0;
            int id = 1;
            for (UICrumbsDateAndTime datatime : request.getUicrumbsDateAndTimes()) {
                if (index == 0) {
                    for (int i = 9; i < 19; i++) {
                        Date timestart = datatime.getDateBegin();
                        Date timeend = datatime.getDateEnd();
                        String st = DateTimeFormat.getFormat(ConstantsHolder.getConstants().DATE_FORMAT_FULL()).format(timestart);
                        String end = DateTimeFormat.getFormat(ConstantsHolder.getConstants().DATE_FORMAT_FULL()).format(timeend);
                        int s = DateTimeFormat.getFormat(ConstantsHolder.getConstants().DATE_FORMAT_FULL()).parse(st).getHours();
                        int e = DateTimeFormat.getFormat(ConstantsHolder.getConstants().DATE_FORMAT_FULL()).parse(end).getHours();
                        if (i == s) {

                            startTimeList.setSelectedIndex(id);
                            startTimeList.setEnabled(false);
                            start_time = startTimeList.getItemText(id);
                            id++;
                        } else if (i == e) {

                            endTimeList.setSelectedIndex(id);
                            endTimeList.setEnabled(false);
                            end_time = endTimeList.getItemText(id);
                            id++;
                        } else {
                            id++;
                        }
                    }
                    String dateString = DateTimeFormat.getFormat(ConstantsHolder.getConstants().DATE_FORMAT_SHORT()).format(datatime.getDateBegin());
                    text.setText(dateString);
                } else break;
            }
        }


        if (request.getUicrumbsComments().size() != 0) {

            StringBuffer AllComments = new StringBuffer("");
            int size = request.getUicrumbsComments().size();
            comment_content = new Grid(size, 3);
            comment_content.setCellSpacing(6);
            int i = 0;
            for (UIComment comment : request.getUicrumbsComments()) {
                if (comment.getComment() != null && comment.getComment() != "") {
                    comment_content.setWidget(i, 0, new Label(comment.getCommenter().getSurnmNmSecondnm()));
                    comment_content.setWidget(i, 1, new Label(comment.getCommentDate()));
                    comment_content.setWidget(i, 2, new Label(comment.getComment()));

                    i++;
                }
            }

            commentField = new DisclosurePanel("See request's comment history");
            commentField.setAnimationEnabled(true);
            commentField.setContent(comment_content);
            cField.add(commentField);


        } else comments.setText("NO COMMENTS");
        try {
            if (request.getState().equals(constants.inProgressState()) &&
                    request.getEngineer() != null) {
                this.sendForProcessingButton.setEnabled(true);
            }
            if (request.getState().equals(constants.needMoreInfoState())) {
                this.assignButton.setEnabled(false);
            }
            if (request.getEngineer() != null) {
                this.labelAssignedEngineer.setText(constants.ASSIGNED_ENGINEER());
                if (request.getEngineer().getPhone() != null)
                    this.AssignedEngineer.setText(request.getEngineer().getSurnmNmSecondnm().concat(" (" + request.getEngineer().getPhone() + ")"));
                else this.AssignedEngineer.setText(request.getEngineer().getSurnmNmSecondnm());
            }

        } catch (NullPointerException eng) {
        }

    }


    public void addeWorkField() {

        buttonForCatalog.add(this.catalog);

        int numRows = flexTable.getRowCount();
        int i = 0;
        for (int key : workList.keySet()) {

            flexTable.setWidget(i, 0, new Label(workList.get(key).getWorkName()));
            try {
                flexTable.setWidget(i, 1, new Label("" + amountsOfWorkValues.get(key)));
            } catch (NumberFormatException e) {

                flexTable.setWidget(i, 1, new Label("" + 1));
            }
            i++;
        }

        flexTable.getFlexCellFormatter().setRowSpan(0, 1, numRows + 1);
    }

    private void setInnerStyle() {
        //Input field that could be edited
        comments.addStyleName(Recources.INSTANCE.styles().TextAreaWritable());
    }

}